﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_bug_BugAssignment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Master.Master.CurrentUserManagement.CurrentUser.Role == (int)UserManagement.Role.Admin)
            {
                var projectNames = from item in ApplicationGlobalVariables.KillDaBugsDatabase.Projects
                                   select new
                                   {
                                       item.Name,
                                       item.ProjectId
                                   };
                if (projectNames.Any())
                    foreach (var project in projectNames)
                        ddlProjectName.Items.Add(new ListItem(project.Name, project.ProjectId.ToString()));
                else
                    ddlProjectName.Items.Add(new ListItem("You have no project yet", "-1"));
            }
            else if (Master.Master.CurrentUserManagement.CurrentUser.Role == (int)UserManagement.Role.ProjectLead)
            {
                var projectNames = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects
                                   where item.Username == Master.Master.CurrentUserManagement.CurrentUser.Username
                                   select new
                                   {
                                       item.Project.Name,
                                       item.Project.ProjectId
                                   };
                if (projectNames.Any())
                    foreach (var project in projectNames)
                        ddlProjectName.Items.Add(new ListItem(project.Name, project.ProjectId.ToString()));
                else
                    ddlProjectName.Items.Add(new ListItem("You have assigned project yet", "-1"));
            }
        }
    }

    protected void ddlProjectName_SelectedIndexChanged(object sender, EventArgs e)
    {
        var query = from item in ApplicationGlobalVariables.KillDaBugsDatabase.Bugs
                    where item.ProjectId == int.Parse(ddlProjectName.SelectedValue)
                    orderby item.LastUpdatedDate descending
                    select new
                    {
                        item.Type,
                        item.BugId,
                        item.Severity,
                        item.Summary,
                        item.Reproducability,
                        item.Status,
                        item.Priority,
                        item.LastUpdatedDate,
                        item.Description,
                        userList = item.User_Bugs.Where(i => i.BugId == item.BugId).Select(i => i.User)
                    };

        lvView.DataSource = query;
        lvView.DataBind();

        // get users active in current project
        var usernames = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects
                        where item.ProjectId == int.Parse(ddlProjectName.SelectedValue)
                        select item.Username;

        ddlUser.Items.Clear();

        foreach (var username in usernames)
            ddlUser.Items.Add(username);
    }

    protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        BugManagement bugMan = new BugManagement(Master.Master.CurrentUserManagement.CurrentUser);
        if (bugMan.AssignUserToBug(ddlUser.SelectedValue, int.Parse(txtBugId.Text))) ;
    }
}